package com.agricultural.machine.factory;

import com.agricultural.machine.common.IMachine;
import com.agricultural.machine.common.MachineState;
import com.agricultural.machine.state.IdleState;
import com.agricultural.machine.state.EmergencyStoppedState;

import java.util.UUID;

/**
 * 抽象农机类 - 工厂方法模式的产品
 */
public abstract class AbstractMachine implements IMachine {
    private final String machineId;
    private final String type;
    private MachineState state;
    private double posX;
    private double posY;
    private boolean emergencyStopped;

    public AbstractMachine(String type) {
        this.machineId = UUID.randomUUID().toString().substring(0, 8);
        this.type = type;
        this.state = new IdleState();
        this.posX = 0.0;
        this.posY = 0.0;
        this.emergencyStopped = false;
    }

    @Override
    public String getMachineId() {
        return machineId;
    }

    @Override
    public String getType() {
        return type;
    }

    @Override
    public MachineState getState() {
        return state;
    }

    @Override
    public void setState(MachineState state) {
        this.state = state;
        System.out.println("农机 " + machineId + " 状态变更为：" + state.getDescription());
    }

    @Override
    public void start() {
        if (emergencyStopped) {
            System.out.println("农机 " + machineId + " 处于紧急停止状态，无法启动");
            return;
        }
        state.start(this);
    }

    @Override
    public void stop() {
        state.stop(this);
    }

    @Override
    public void executeTask(String task) {
        if (emergencyStopped) {
            System.out.println("农机 " + machineId + " 处于紧急停止状态，无法执行任务");
            return;
        }
        state.executeTask(this, task);
    }

    @Override
    public void updatePosition(double x, double y) {
        this.posX = x;
        this.posY = y;
        System.out.println("农机 " + machineId + " 位置更新为 (" + x + ", " + y + ")");
    }

    @Override
    public String getPosition() {
        return "(" + posX + ", " + posY + ")";
    }

    @Override
    public double getCurrentX() {
        return posX;
    }

    @Override
    public double getCurrentY() {
        return posY;
    }

    @Override
    public void work() {
        if (state.getDescription().equals("工作中")) {
            System.out.println("农机 " + machineId + " 正在工作中");
        } else {
            System.out.println("农机 " + machineId + " 当前状态为" + state.getDescription() + "，无法工作");
        }
    }

    @Override
    public boolean isRunning() {
        return state.getDescription().equals("工作中");
    }

    @Override
    public void setEmergencyStop(boolean emergency) {
        this.emergencyStopped = emergency;
        if (emergency) {
            this.state = new EmergencyStoppedState();
            System.out.println("农机 " + machineId + " 进入紧急停止状态");
        } else {
            this.state = new IdleState();
            System.out.println("农机 " + machineId + " 解除紧急停止状态");
        }
    }

    @Override
    public boolean isEmergencyStopped() {
        return emergencyStopped;
    }
}


















